<?php
    
    $weekID = isset($_GET['w']) && is_numeric($_GET['w']) ? $_GET['w'] : -1;
    $competitionID = isset($_GET['c']) && is_numeric($_GET['c']) ? $_GET['c'] : -1;

    require '../Include/BDMySQL.class.php';
    $MySQL = new BDMySQL();
    $MySQL->ligarBD() or die;
        
    header('Content-Type: application/xml;'); 
    
    $XML = new SimpleXMLElement_Plus('<?xml version="1.0" encoding="utf-8"?> <XSWeekDetails
        xmlns="http://rpmestre.net/xscrawler"
        xmlns:XSWeekDetails = "http://www.w3.org/2001/XMLSchema-instance"
        XSWeekDetails:schemaLocation = "http://rpmestre.net/xscrawler XSWeekDetailsSchema.xsd">
        </XSWeekDetails>');
        if ($weekID>0)$XML->addAttribute('weekID', 'w_'.$weekID);
        if ($competitionID>0) $XML->addAttribute('competitionID', 'c_'.$competitionID);

    
   $XML->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="xslt/gamelistpreview_page.xslt"'); 

      
    $sql =" SELECT e1.nome,  e2.nome, j.*, c.*, s.*
            FROM jogos j, equipas e1, equipas e2, edicoescompeticoes ec, epocas s, competicoes c
            WHERE DATE_FORMAT(CONCAT(j.dia,' ', j.hora), '%Y-%m-%d %H:%i:%s')>= DATE_FORMAT(now(), '%Y-%m-%d %H:%i:%s')
            AND j.idCasa=e1.idEquipa 
            AND j.idFora=e2.idEquipa
            AND j.idEdicaoCompeticao= ec.idEdicaoCompeticao
            AND ec.idEpoca = s.idEpoca
            AND ec.idCompeticao  = c.idCompeticao
            AND j.idEdicaoCompeticao = 8 
            order by j.dia asc, j.idEdicaoCompeticao desc, j.jornada desc, j.dia desc, j.hora asc, j.idJogo desc LIMIT 0,10";

    $query = mysql_query($sql);
    $weekList = array();
	while ($row = mysql_fetch_array($query, MYSQL_NUM)) 
	{
        $XML_match = $XML->addChild('match');
        $XML_match->addAttribute('id', 'm_'.$row[2]);
        
        
        $XML_homeTeam = $XML_match->addChild('host', $row[0]);
        $XML_homeTeam->addAttribute('id', 'xst_'.$row[8]);
        $XML_awayTeam = $XML_match->addChild('visitor', $row[1]);
        $XML_awayTeam->addAttribute('id', 'xst_'.$row[9]);
                
        
		$XML_week = $XML_match->addChild('weekDescription', $row[5]==''?$row[4]:$row[5]);
        $XML_week->addAttribute('week', 'w_'.$row[4]);
        
		$XML_CompetitionEdition = $XML_match->addChild('competition', $row[15].' '.$row[18]);
        $XML_CompetitionEdition->addAttribute('id', 'c_'.$row[3]);
        
        $XML_match->addChild('date', $row[6]);
        $XML_match->addChild('hour', $row[7]);        			
    }
    echo $XML->asXML();
    
    
    class SimpleXMLElement_Plus extends SimpleXMLElement 
    {
        public function addProcessingInstruction( $name, $value )
        {
            // Create a DomElement from this simpleXML object
            $dom_sxe = dom_import_simplexml($this);
           
            // Create a handle to the owner doc of this xml
            $dom_parent = $dom_sxe->ownerDocument;
           
            // Find the topmost element of the domDocument
            $xpath = new DOMXPath($dom_parent);
            $first_element = $xpath->evaluate('/*[1]')->item(0);
           
            // Add the processing instruction before the topmost element           
            $pi = $dom_parent->createProcessingInstruction($name, $value);
            $dom_parent->insertBefore($pi, $first_element);
        }
    }
 ?>